var week_span_dates = [];
var driver_list = '';

function getSiteCfg() { 
    $.getJSON(
        "/api/tmpo/config_public/",
        function(json){
            var val = 'POC: <a class="POCemail" href="mailto:' +
                      json.tmpo_poc_email + '">' + json.tmpo_poc_name+'</a> '+
                      'Phone: (' + json.tmpo_poc_phone.substr(0,3) + ') '+
                      json.tmpo_poc_phone.substr(3,3) + '-' + 
                      json.tmpo_poc_phone.substr(6,4);
            $("span.POCemail").append(val); 
        }
    ); 
}

function bindRows() {
    $.map(driver_list, function(row){
        $("#tbody_" + row.id).hover(
            function () { 
                $("#tbody_" + row.id).addClass('highlight'); 
            },
            function () { 
                $("#tbody_" + row.id).removeClass('highlight'); 
            }
        );
		$("#span_detail_"+ row.id).click(
        	function () {
                location.href = "/tmpo/drivers/detail/?id="+row.id;
            }
    	);
    });
}

function displayDriverSchedule(first_day,last_day) {
	var array_drivers = [];
	var array_drivers_id = [];
	var array_commitments = [];
	var array_commitments_today = [];
	var array_du = [];	
	$.getJSON(
        '/api/tmpo/drivers/',
        function(json) {
            driver_list = $(json.drivers);
            $.map(json.drivers,function(n,i){
                array_drivers[i]= n.name;
                array_drivers_id[i]= n.id;
            });
        }
    );	
	$("#driverSchedule tbody").remove();    
    $.getJSON(
        '/api/tmpo/du/' + first_day+  ' 00:00/' + last_day + ' 23:59/',
        function(json) {
            array_du = $.makeArray(json.events); 
        }
    );
    $.getJSON(
        '/api/tmpo/schedule/'+first_day+' 00:00/' +last_day+ ' 23:59/',
        function(json) {            
            $.map(array_drivers_id,function(n,i){         
                $("#driverSchedule").append(
                    "<tbody id='tbody_"+n+"'><tr><td>"+
                    "<span id=span_detail_"+n+" class='hoverButton'>Detail"+
                    "</span>"+array_drivers[i]+"</td><td id='"+ n + "_" + 
                    week_span_dates[0] + "'></td><td id='"+ n + "_" + 
                    week_span_dates[1] + "'></td><td id='"+ n + "_" + 
                    week_span_dates[2] + "'></td><td id='"+ n + "_" + 
                    week_span_dates[3] + "'></td><td id='"+ n + "_" + 
                    week_span_dates[4] + "'></td><td id='"+ n + "_" + 
                    week_span_dates[5] + "'></td><td id='"+ n + "_" + 
                    week_span_dates[6] + "'></td></tr></tbody>"); 
                array_commitments = $.grep(json.item,function(n,x){
                    return (n.driver_id == array_drivers_id[i]);							  
                });                
                array_commitments_today = $.grep(array_commitments,
                function(n,x){
                    return (n.start.substr(0,10) == week_span_dates[0] || 
                    n.start.substr(0,10) == week_span_dates[1] || 
                    n.start.substr(0,10) == week_span_dates[2] || 
                    n.start.substr(0,10) == week_span_dates[3] || 
                    n.start.substr(0,10) == week_span_dates[4] || 
                    n.start.substr(0,10) == week_span_dates[5] || 
                    n.start.substr(0,10) == week_span_dates[6] );							  
                });                
                $.map(array_commitments_today,function(n,x){              
                    $("#"+n.driver_id+'_'+
                    n.start.substr(0,10)).append(
                    n.start.replace(/:|-| /g, "").substr(8,4)+
                    ' - '+n.finish.replace(/:|-| /g, "").substr(8,4)+
                    '  '+n.location_delivery +'<br><br>');                  
                    $("#"+n.driver_id+'_'+
                    n.start.substr(0,10)).addClass("dsOfficeColor");
                }); 
                array_commitments = $.grep(array_du,function(n,x){          
                    return (n.driver_id == array_drivers_id[i]);							  
                });              
                array_commitments_today = $.grep(array_commitments,
                function(n,x){
                    return (n.start.substr(0,10) == week_span_dates[0] || 
                    n.start.substr(0,10) == week_span_dates[1] || 
                    n.start.substr(0,10) == week_span_dates[2] || 
                    n.start.substr(0,10) == week_span_dates[3] || 
                    n.start.substr(0,10) == week_span_dates[4] || 
                    n.start.substr(0,10) == week_span_dates[5] || 
                    n.start.substr(0,10) == week_span_dates[6] );				  
                });              
                $.map(array_commitments_today,function(n,x){             
                    $("#" + n.driver_id + '_' + 
                    n.start.substr(0,10)).append(n.description+' '+ 
                    n.start.replace(/:|-| /g, "").substr(8,4)+' - '+ 
                    n.end.replace(/:|-| /g, "").substr(8,4)+'<br>');
                $("#"+n.driver_id + '_'+ 
                    n.start.substr(0,10)).addClass("dsUnavailableColor");
                });   
            });
            bindRows();	
        }
    );	
}

function getDriverScheduleDate(a) {
    var today_check = '';
    var html_column_bulider = '';
    $.getJSON(
        '/api/dates/week/' + a + '/',
        function(json) {
            today_check = json.info[0].today;            
            $.map(json.weekspan,function(n,i)
            {            
                week_span_dates[0]= n.comp_first;
                week_span_dates[1]= n.comp_second;
                week_span_dates[2]= n.comp_third;
                week_span_dates[3]= n.comp_fourth;
                week_span_dates[4]= n.comp_fifth;
                week_span_dates[5]= n.comp_sixth;
                week_span_dates[6]= n.comp_seventh;
            });            
            $("#driverSchedule caption").html('<span class="captionDate">'+
                '<a href="?date=' + json.info[0].previous_week + '">'+
                '<img src="/tmpo/images/icons/previous.gif" '+
                'alt="Go to Previous'+
                ' Week" width="16" height="16" /></a> '+
                json.weekspan[0].first+' - '+json.weekspan[0].seventh+
                ' <a href="?date='+
                json.info[0].next_week+'">'+
                '<img src="/tmpo/images/icons/next.gif"'+
                'alt="Go to Next Week" width="16" height="16"  /></a>'+
                '</span> Schedule of Drivers');
            $("#driverSchedule thead").remove();        
            $("#driverSchedule").append(
                '<thead><tr><th scope="col">Driver</th>'+
                '<th scope="col" >'+json.weekspan[0].first_day+'<br />'+ 
                json.weekspan[0].first+'</th><th scope="col" >'+ 
                json.weekspan[0].second_day+'<br />'+json.weekspan[0].second+
                '</th><th scope="col">'+json.weekspan[0].third_day+'<br />'+
                json.weekspan[0].third+'</th><th scope="col">'+
                json.weekspan[0].fourth_day+'<br />'+json.weekspan[0].fourth+
                '</th><th scope="col">'+json.weekspan[0].fifth_day+'<br />'+
                json.weekspan[0].fifth+'</th><th scope="col">'+
                json.weekspan[0].sixth_day+'<br />'+json.weekspan[0].sixth+ 
                '</th><th scope="col">'+json.weekspan[0].seventh_day+'<br />'+
                json.weekspan[0].seventh+'</th></tr></thead>');                
            html_column_bulider = '<col id="dsDriverCol" />';            
            $.map(week_span_dates,function(n,x){
                if( n == today_check){
                    html_column_bulider += '<col id="dsTodayCol" />';
                    $('th:eq('+(x+1)+')').addClass("tableCurrentHeadColor");
                }
                else{
                    html_column_bulider += '<col id="dsDay'+(x+1)+'Col" />';
                }            
            });              
            $("#driverSchedule colgroup").html(html_column_bulider);            
            displayDriverSchedule(json.weekspan[0].comp_first,
                                  json.weekspan[0].comp_seventh);		
         }
    );
}

function getToday() {
    var url_api_get_dates = '/api/dates/';	
    if($.qs('date')){
        url_api_get_dates = url_api_get_dates + 
        $.qs('date') + '/';
    }
    $.getJSON(
        url_api_get_dates,
        function(json) {
            getDriverScheduleDate(json.info[0].today);
        }
    );
}

function sendMakePrinter(element) {
    var closer = function() {
        $("span[class='hoverButton']").remove();
        makePrinter("#"+element)();
    };
    return closer;   
}

$(function() {
    Functional.install();
    getSiteCfg();
    getToday();
    $("#button_print").click(sendMakePrinter("driverSchedule") );
});